package com.wd.demo1.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.wd.demo1.i18.DemoBundle;
import com.wd.demo1.icon.PluginIcons;
import com.wd.demo1.persistent.DemoPersistent;
import com.wd.demo1.utils.FileChooseUtils;
import com.wd.demo1.utils.NotificationUtils;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Administrator
 * @date 2025-10-23 0:45
 */
public class MainUI extends DialogWrapper {

	private JPanel rootPanel;
	private JLabel pathLabel;
	private JTextField pathTextField;
	private JButton selectButton;
	private JButton okButton;
	private JButton cancelButton;
	private Project project;

	public MainUI(@Nullable Project project) {
		super(project);
		this.project = project;
		setTitle("Demo1");
		setModal(true);
		init();
		//DemoPersistent persistent = DemoPersistent.getInstance(project);
		DemoPersistent persistent = DemoPersistent.getInstance();
		if (StringUtils.isNotBlank(persistent.getPath())) {
			pathTextField.setText(persistent.getPath());
		}
		selectButton.setText(DemoBundle.message("select"));
		selectButton.setIcon(PluginIcons.open);
		cancelButton.setIcon(PluginIcons.warningDialog);
		okButton.setIcon(PluginIcons.successDialog);

		//获取项目路径
		VirtualFile virtualFile = ProjectUtil.guessProjectDir(project);

		cancelButton.addActionListener(e -> dispose());
		okButton.addActionListener(e -> {
			JOptionPane.showMessageDialog(rootPanel, "选择的路径为：" + pathTextField.getText());
		});
		selectButton.addActionListener(e -> {
			FileChooseUtils instance = FileChooseUtils.getInstance(project);
			VirtualFile vf = instance.showSingleFileSelectionDialog("选择文件", virtualFile);
			if (vf != null) {
				//选择了文件
				String path = vf.getPath();
				pathTextField.setText(path);
				persistent.setPath(path);
			} else {
				//没有选择文件
				NotificationUtils.showInfoNotification(project, "提示：", "请选择文件");
			}
		});
	}

	@Override
	protected void dispose() {
		super.dispose();
	}

	@Override
	protected Action @NotNull [] createActions() {
		return new Action[0];
	}

	@Override
	protected @Nullable JComponent createCenterPanel() {
		return rootPanel;
	}
}
